﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 商户收款控制器
    /// </summary>
    public class ReceivablesController : BaseController
    {
        private readonly IReceivablesBusiness ReceivablesBusiness;
        private readonly IMerchantBusiness merchantBusiness;
        public ReceivablesController(IReceivablesBusiness _ReceivablesBusiness, IMerchantBusiness _merchantBusiness)
        {
            ReceivablesBusiness = _ReceivablesBusiness;
            merchantBusiness = _merchantBusiness;
        }

        /// <summary>
        /// 商户收款管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            string merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                var data = merchantBusiness.GetNoDelInfoByUserId(UserHelper.GetCurrentUserId());
                if (data.status_code == 200)
                {
                    //如果查询到内容，将其保存到Session中
                    UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
                    userInfoSession.MerchantId = data.data.Id;
                    userInfoSession.MerchantName = data.data.Name;
                    UserHelper.SetSession(userInfoSession);

                    merchantId = data.data.Id;
                }
                else
                {
                    merchantId = "";
                }
            }

            ViewBag.MerchantId = merchantId;
            ViewBag.MerchantName = UserHelper.GetCurrentUser().MerchantName;
            return View();
        }

        /// <summary>
        /// 根据查询条件查询商户收款管理
        /// </summary>
        /// <param name="receivablesSearch">商户收款管理查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(ReceivablesSearch receivablesSearch)
        {
            receivablesSearch.MerchantId = UserHelper.GetCurrentUser().MerchantId;
            var data = ReceivablesBusiness.List(receivablesSearch);
            return JsonDateTime(data.data);
        }

        /// <summary>
        /// 根据当前登录的商户Id查询商户下的所有付款方式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetReceivablesByMerchantId()
        {
            string merchantId = UserHelper.GetCurrentUser().MerchantId;
            var data = ReceivablesBusiness.GetReceivablesByMerchantId(merchantId);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加商户收款管理
        /// </summary>
        /// <param name="entity">商户收款管理实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Add(ReceivablesEntity entity)
        {
            entity.MerchantId = UserHelper.GetCurrentUser().MerchantId;
            var data = ReceivablesBusiness.Add(entity);
            return Json(data);
        }

        /// <summary>
        /// 修改商户收款管理
        /// </summary>
        /// <param name="entity">商户收款管理实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Edit(ReceivablesEntity entity)
        {
            var data = ReceivablesBusiness.Update(entity);
            return Json(data);
        }

        /// <summary>
        /// 设置默认非默认
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <param name="DefaultMark">默认非默认</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SetDefaultMark(string Id, int DefaultMark)
        {
            var data = ReceivablesBusiness.SetDefaultMark(Id, DefaultMark);
            return Json(data);
        }
        
        /// <summary>
        /// 删除收款
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Delete(string id)
        {
            var data = ReceivablesBusiness.Delete(id);
            return Json(data);
        }
    }
}
